<template>
  <div class="ele-body">
    <!-- 搜索表单 -->

    <el-card shadow="never">
      <!-- 数据表格 -->
      <stream-search @search="reload" />

      <el-card shadow="never" body-style="padding: 15px 0 25px 0;">
        <el-row :gutter="20" style="margin-bottom: 20px">
          <el-col :md="24" :sm="12" :xs="24">
            <el-alert type="info" :closable="false" class="ele-alert-border">
              <span class="text-color">
                订单总金额:<span class="text-blue">  {{ order_amount }}</span>
              </span>
              <span class="text-color"  v-if="platform_id == 0">
                总平台利润:<span class="text-blue"> {{ system_handling_fee }}</span>
              </span>
                <span class="text-color">
                子平台利润:<span class="text-blue"> {{ platform_application_rate }}</span>
              </span>
              
              
            </el-alert>
          </el-col>
        </el-row>
      </el-card>

      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
      >
        <template v-slot:user="{ row }">
          <div class="ele-cell" v-if="row.userInfo">
            <el-avatar
              v-if="row.userInfo.avatar"
              :src="row.userInfo.avatar"
              shape="square"
              :size="40"
            />
            <div class="ele-cell-content">
              <div class="ele-cell-title">{{ row.userInfo.nickname }} </div>
              <div class="ele-cell-desc">{{ row.userInfo.telphone }} </div>
            </div>
          </div>
          <span v-else>无</span>
        </template>

        <template v-slot:sotre="{ row }">
          <div class="ele-cell">
            <div class="ele-cell" v-if="row.storeInfo">
              <div class="ele-cell-content">
                <div class="ele-cell-title">{{ row.storeInfo.telphone }} </div>
                <div class="ele-cell-title"
                  >{{ row.storeInfo.store_name }}
                </div>
              </div>
            </div>
          </div>
        </template>

        <template v-slot:order_type="{ row }">
          <ele-dot
            v-if="row.order_type == 1"
            type="warning"
            effect="plain"
            text="核销优惠券/消费券"
            :ripple="true"
          />
          <ele-dot
            v-if="row.order_type == 2"
            type="success"
            effect="plain"
            text="购买优惠券"
            :ripple="true"
          />
        </template>

        <template v-slot:platformInfo="{ row }">
          <div class="ele-cell" v-if="row.platformInfo">
            <div class="ele-cell-content">
              <div class="ele-cell-title">{{ row.platformInfo.name }} </div>
              <div class="ele-cell-desc">{{ row.platformInfo.telphone }} </div>
            </div>
          </div>
          <span v-else>无</span>
        </template>

        <!-- 表头工具栏 -->
        <!-- 状态列 -->
        <template v-slot:status="{ row }">
          <el-switch
            :active-value="1"
            :inactive-value="2"
            v-model="row.status"
            @change="editStatus(row)"
          />
        </template>

        <stream-edit :visible.sync="showEdit" :data="current" @done="reload" />
        <!-- 状态列 -->
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
  </div>
</template>

<script>
import { list, remove, modify, sortChange, doDefault } from '@/api/stream';
import streamSearch from './components/stream-search.vue';
import streamEdit from './components/stream-edit';
import Sortable from 'sortablejs';

export default {
  name: 'stream',
  components: {
    streamSearch,
    streamEdit
  },
  created() {
    this.platform_id = localStorage.getItem('platform_id');
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: 'left'
        },

        {
          prop: 'platformInfo',
          label: '平台详情',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'platformInfo',
          show: localStorage.getItem('platform_id') == 0 ? true : false
        },

        {
          prop: 'order_number',
          label: '订单号',
          showOverflowTooltip: true,
          minWidth: 150
        },
        {
          prop: 'user',
          label: '用户信息',
          showOverflowTooltip: true,
          minWidth: 150,
          slot: 'user',
          show: localStorage.getItem('platform_id') == 0 ? true : false
        },
        {
          prop: 'sotre',
          label: '店铺信息',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'sotre',
          show: localStorage.getItem('platform_id') == 0 ? true : false
        },

        {
          prop: 'order_amount',
          label: '订单总金额',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'actual_amount',
          label: '店铺到款',
          showOverflowTooltip: true,
          minWidth: 110
        },

        {
          prop: 'order_type',
          label: '订单类型',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'order_type'
        },

        {
          prop: 'system_application_rate',
          label: '站点手续费比例',
          showOverflowTooltip: true,
          minWidth: 120,
          show: localStorage.getItem('platform_id') == 0 ? true : false
        },
        {
          prop: 'system_handling_fee',
          label: '站点手续费',
          showOverflowTooltip: true,
          minWidth: 110,
          show: localStorage.getItem('platform_id') == 0 ? true : false
        },

        {
          prop: 'platform_application_rate',
          label: '平台手续费比例',
          showOverflowTooltip: true,
          minWidth: 110
        },

        {
          prop: 'platform_handling_fee',
          label: '平台手续费',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'platform_handling_fee'
        }
      ],
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示分配回收员
      showCollector: false,
      platform_id: '',
       order_amount: '',
       system_handling_fee: '',
       platform_application_rate: '',


      //弹框名称
      id: 0
    };
  },

  methods: {
    rowDrop() {
      const tbody = document.querySelector('.el-table__body-wrapper tbody');
      const _this = this;
      Sortable.create(tbody, {
        forceFallback: true,
        dragClass: 'drag',
        delay: 100,
        animation: 1000,
        onEnd({ newIndex, oldIndex }) {
          //更换表格数据 后续拖动坐标才会正确
          let data = _this.$refs.table.getData();

          let nid = data[newIndex].id;
          let oid = data[oldIndex].id;
          const currRow = data.splice(oldIndex, 1)[0];
          data.splice(newIndex, 0, currRow);

          sortChange(data)
            .then((msg) => {})
            .catch((e) => {
              _this.$refs.table.reload();
            });
          //去数据库更换排序
        }
      });
    },
    /* 表格数据源 */
    datasource({ page, limit, where, order }) {
    //  this.order_amount = data.data.order_amount;
    //     this.system_handling_fee = data.data.system_handling_fee;
    //     this.platform_application_rate = data.data.platform_application_rate;
      var dataList =  list({ ...where, ...order, page, limit });
     dataList.then((res)=>{
       this.order_amount = res.data.order_amount;
        this.system_handling_fee = res.data.system_handling_fee;
         this.platform_application_rate = res.data.platform_application_rate;
      });


      return dataList
    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({ page: 1, where: where });
    },
    /* 打开编辑弹窗 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      remove(row.id)
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },

    /* 一键默认 */
    doDefault() {
      this.$confirm(
        '请谨慎使用该功能，一旦使用则会变成系统默认内容。确定一键默认？',
        '提示',
        {
          type: 'warning'
        }
      )
        .then(() => {
          const loading = this.$loading({ lock: true });
          doDefault()
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },

    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的数据吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          remove(this.selection.map((d) => d.id))
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },

    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true });
      modify({ id: row.id, field: 'status', value: row.status })
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          row.status = row.status == 1 ? 2 : 1;
          this.$message.error(e.message);
        });
    },
    /* 更改状态 */
    editRecommend(row) {
      const loading = this.$loading({ lock: true });
      modify({ id: row.id, field: 'is_recommend', value: row.is_recommend })
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          row.is_recommend = row.is_recommend == 1 ? 2 : 1;
          this.$message.error(e.message);
        });
    }
  },
  mounted() {
    this.rowDrop();
  }
};
</script>

<style>

.drag {
  background: #000 !important;
  background-image: linear-gradient(#333, #999) !important;
}
</style>



<style lang="less" scoped>

    .text-blue {
        color: #1890ff;
    }

    .text-color {
        color: #000;
        font-size: 16px;
        width: 245px;
        display: inline-block;
    }

    .info-desc {
        color: #999999;
        font-size: 14px;
    }

    .package-name {
        color: #1367de;
        font-size: 14px;
        margin-right: 5px;
    }

    .package-money {
        color: #ff0000;
        font-size: 18px;
        margin-right: 5px;
    }

    .drag {
        background: #000 !important;
        background-image: linear-gradient(#333, #999) !important;
    }

    .ele-cell-content {
        overflow: hidden;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
    }
</style>

